package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import com.atguigu.tingshu.vo.account.AccountLockResultVo;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account/userAccount")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserAccountApiController {

	@Autowired
	private UserAccountService userAccountService;

	/**
	 * 检查及扣减账户余额
	 * @param accountDeductVo
	 * @return
	 */
	@GuiGuLogin(required = false)
	@Operation(summary = "检查及扣减账户余额")
	@PostMapping("/checkAndDeduct")
	public Result checkAndDeduct(@RequestBody AccountDeductVo accountDeductVo){
		//	调用服务此方法.
		userAccountService.checkAndDeduct(accountDeductVo);
		//	返回数据
		return Result.ok();
	}


	/**
	 * 查看用户消费记录
	 * @param page
	 * @param limit
	 * @return
	 */
	@GuiGuLogin
	@Operation(summary = "获取用户消费记录")
	@GetMapping("findUserConsumePage/{page}/{limit}")
	public Result findUserConsumePage(@PathVariable Long page,
									  @PathVariable Long limit){
		//	获取用户Id
		Long userId = AuthContextHolder.getUserId();
		//	封装一个Page对象
		Page<UserAccountDetail> detailPage = new Page<>(page,limit);
		//	调用服务层方法.
		IPage<UserAccountDetail> iPage = userAccountService.findUserConsumePage(detailPage,userId);
		//	返回数据
		return Result.ok(iPage);
	}

	/**
	 * 用户充值记录
	 * @param page
	 * @param limit
	 * @return
	 */
	@GuiGuLogin
	@Operation(summary = "获取用户充值记录")
	@GetMapping("/findUserRechargePage/{page}/{limit}")
	public Result findUserRechargePage(@PathVariable Long page,
									   @PathVariable Long limit){
		//	获取用户Id
		Long userId = AuthContextHolder.getUserId();
		//	封装一个Page对象
		Page<UserAccountDetail> detailPage = new Page<>(page,limit);
		//	调用服务层方法.
		IPage<UserAccountDetail> iPage = userAccountService.findUserRechargePage(detailPage,userId);
		//	返回数据
		return Result.ok(iPage);
	}

	/**
	 * 检查锁定账户金额
	 * @param accountLockVo
	 * @return
	 */
	@GuiGuLogin
	@Operation(summary = "检查与锁定账户金额")
	@PostMapping("checkAndLock")
	public Result checkAndLock(@RequestBody AccountLockVo accountLockVo){
		//	调用服务层方法.
		AccountLockResultVo accountLockResultVo = this.userAccountService.checkAndLock(accountLockVo);
		//	返回数据
		return Result.ok(accountLockResultVo);
	}

	/**
	 * 获取账户可用余额
	 * @return
	 */
	@GuiGuLogin
	@Operation(summary = "获取账号可用金额")
	@GetMapping("getAvailableAmount")
	public Result getAvailableAmount(){
		//	调用服务层方法.
		Long userId = AuthContextHolder.getUserId();
		BigDecimal amount = userAccountService.getAvailableAmount(userId);
		//	返回数据
		return Result.ok(amount);
	}

}

